package com.weimob.prettystore.util;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;

import java.util.UUID;

/**
 * 辅助工具
 *
 * @author ljgLiu
 * @date 2017/11/13
 */
@Slf4j
public class TrackIdUtil {

    public static final String trackId = "trackId";

    /**
     * 设置线程trackId
     *
     * @param preFix 前缀
     * @param sufFix 后缀
     */
    public static void setTrackId(String preFix, String sufFix) {
        try {
            if (MDC.get(trackId) != null) {
                return;
            }
            preFix = (preFix == null ? "" : preFix);
            sufFix = (sufFix == null ? "" : sufFix);
            // 设置线程trackId
            String uid = UUID.randomUUID().toString();
            MDC.put(trackId, preFix + uid + sufFix);
        } catch (Exception e) {
            log.error("thread setTrackId Error!", e);
        }
    }

    /**
     * 清理线程trackId
     */
    public static void clearTrackId(String preFix) {
        try {
            // 兼容Soa
            if (preFix != null && !preFix.isEmpty()) {
                String id = MDC.get(trackId);
                if (id != null && id.startsWith(preFix)) {
                    MDC.remove(trackId);
                }
                return ;
            }
            MDC.remove(trackId);
        } catch (Exception e) {




            log.error("thread clearTrackId Error!mybranch", e);


            log.error("thread clearTrackId Error!masterhaha", e);



        }
    }



}
